//====================================================
function ObjStk(parent_, g_var) {
	this.g_var = g_var;
	this.parent_ = parent_;
}
ObjStk.prototype.Init = function(id, name) {
	this.id = id;
	this.name = name;		
}

ObjStk.prototype.GetItu = function() {
	this.itu = new ObjStkItu(this);
	this.itu.Fetch(1);
}
ObjStk.prototype.GetTradeInfo = function() {
	this.tradeInfo = new ObjTradeInfo();
	
	url_ = "http://www.cnyes.com/twstock/div/home_quote_js.asp?code="+this.id;
	//alert(url_);
	_IG_FetchContent(url_, _IG_Callback(Analysis_, this), { refreshInterval: 0});
	function Analysis_(response, objstk) {
		objstk.tradeInfo.parse(response);
		objstk.GetItu();
		//objstk.Filter();
	}
}
ObjStk.prototype.GetAnalysis = function() {
	this.analysis = new ObjAnalysis();

	url_ = "http://megatime.cnyes.com/twstock/"+this.id+".htm";
	//dbgMsg(url_);

	_IG_FetchContent(url_, _IG_Callback(Analysis_, this.analysis, this),  { refreshInterval: 0});
	function Analysis_(response, analysis, objstk) {
		//alert(analysis.shortStar);
		analysis.parse(response);
		objstk.GetTradeInfo();
	}
}
ObjStk.prototype.Filter = function() {
	  if (this.analysis.shortStar >= this.g_var.prefs.getInt("shortStar") 
	  		&& this.analysis.medStar >= this.g_var.prefs.getInt("medStar")
	  		&& this.analysis.longStar >= this.g_var.prefs.getInt("longStar")
				&& this.analysis.shortTrend >= this.g_var.prefs.getInt("shortTrend") 
				&& this.analysis.medTrend >= this.g_var.prefs.getInt("medTrend")
				&& this.analysis.longTrend >= this.g_var.prefs.getInt("longTrend")
				&& this.analysis.opt > 0
				&& this.analysis.per200 >= this.g_var.prefs.getInt("200Per")
				&& this.itu.roe >= this.g_var.prefs.getInt("roe")
				//&& this.tradeInfo.close <= this.g_var.prefs.getInt("price")
				) {

			str1 = "<a href=http://megatime.cnyes.com/twstock/"+this.id+".htm>"+this.id + this.name+"</a>";
			//dbgMsg(str1);
			str2 = this.analysis.shortStar + "/" +
						 this.analysis.medStar + "/" +
						 this.analysis.longStar;
			str3 = this.analysis.perPos + "%/" + this.analysis.per200 + "%"; 
			str4 = "opt: " + this.analysis.opt;
			str5 = "trend: " + this.analysis.shortTrend + "/" + this.analysis.medTrend + "/" + this.analysis.longTrend;
			str10 = "close: " + this.tradeInfo.close + ", " + this.tradeInfo.change;
			str11 = " (" + this.tradeInfo.minOfYear + "-" + this.tradeInfo.maxOfYear + ")";
			str13 = "rgr/ngr/fcfgr: " + this.itu.rgr + "/" + this.itu.ngr + "/" + this.itu.fcfgr;
			str14 = "dr/fa: " + this.itu.dr + "/" + this.itu.fa;
			str15 = "rtar/roe: " + this.itu.rtar + "/" + this.itu.roe;
			str16 = "atr/itr/gptr: " + this.itu.atr + "/" + this.itu.itr + "/" + this.itu.gptr;
			str17 = "per/psr: " + this.itu.per + "/" + this.itu.psr;
			
			this.g_var.dataTable.addRow();
			this.g_var.dataTable.setCell(this.parent_.passCnt, 0, this.id+","+this.name);
			
			this.g_var.dataTable.setCell(this.parent_.passCnt, 1, this.analysis.shortStar);
			
			this.g_var.dataTable.setCell(this.parent_.passCnt, 2, this.analysis.medStar);
			this.g_var.dataTable.setCell(this.parent_.passCnt, 3, this.analysis.longStar);
			
			this.g_var.dataTable.setCell(this.parent_.passCnt, 4, this.analysis.perPos);
			this.g_var.dataTable.setCell(this.parent_.passCnt, 5, this.analysis.per200);
			
			this.g_var.dataTable.setCell(this.parent_.passCnt, 6, this.analysis.shortTrend);
			this.g_var.dataTable.setCell(this.parent_.passCnt, 7, this.analysis.medTrend);
			this.g_var.dataTable.setCell(this.parent_.passCnt, 8, this.analysis.longTrend);
			
			this.g_var.dataTable.setCell(this.parent_.passCnt, 9, this.tradeInfo.close);
			this.g_var.dataTable.setCell(this.parent_.passCnt, 10, this.tradeInfo.change);
			this.g_var.dataTable.setCell(this.parent_.passCnt, 11, this.tradeInfo.minOfYear);
			this.g_var.dataTable.setCell(this.parent_.passCnt, 12, this.tradeInfo.maxOfYear);
			
			this.g_var.dataTable.setCell(this.parent_.passCnt, 13, this.itu.rgr);
			this.g_var.dataTable.setCell(this.parent_.passCnt, 14, this.itu.ngr);
			this.g_var.dataTable.setCell(this.parent_.passCnt, 15, this.itu.fcfgr);
			this.g_var.dataTable.setCell(this.parent_.passCnt, 16, this.itu.dr);
			this.g_var.dataTable.setCell(this.parent_.passCnt, 17, this.itu.fa);
			this.g_var.dataTable.setCell(this.parent_.passCnt, 18, this.itu.rtar);
			this.g_var.dataTable.setCell(this.parent_.passCnt, 19, this.itu.roe);
			this.g_var.dataTable.setCell(this.parent_.passCnt, 20, this.itu.atr);
			this.g_var.dataTable.setCell(this.parent_.passCnt, 21, this.itu.itr);
			this.g_var.dataTable.setCell(this.parent_.passCnt, 22, this.itu.gptr);
			this.g_var.dataTable.setCell(this.parent_.passCnt, 23, this.itu.per);
			this.g_var.dataTable.setCell(this.parent_.passCnt, 24, this.itu.psr);
			this.g_var.dataTable.setCell(this.parent_.passCnt, 25, this.itu.press);
			this.g_var.dataTable.setCell(this.parent_.passCnt, 26, this.itu.support);
		
			this.g_var.stkTbl.draw(this.g_var.dataTable, null);
			_IG_AdjustIFrameHeight();
			
			//stkMsg(str1 + "-> " + str2 + ",  " + str3 + ",  " + str4 + "," + str5 + "," + str10 + str11 +
			//			 "," + str13 + "," + str14 + "," + str15 + "," + str16 + "," + str17); 
			++this.parent_.passCnt;
				
		} 
		else 
		{
			//dbgMsg(this.id+this.name+"-> NG");
		}
		this.parent_.filterIdx++;	
		this.parent_.EnumFiltering();
}

//====================================================
//http://www.otc.org.tw/ch/stock/aftertrading/peratio_analysis/pera.php#
function ObjStkDbTwse(g_var) {
	this.g_var = g_var;
	//var tt = this.g_var.prefs.getInt("shortStar");

	this.objStk = new Array();
	this.max = this.g_var.prefs.getInt("stkMax");
	this.filterIdx = 0;
	alert(this.max);
	
	this.g_var.dataTable.addColumn('string', 'id,name');	//0
	this.g_var.dataTable.addColumn('number', 'ss');				//1
	this.g_var.dataTable.addColumn('number', 'ms');				//2
	this.g_var.dataTable.addColumn('number', 'ls');				//3
	this.g_var.dataTable.addColumn('number', 'p+');
	this.g_var.dataTable.addColumn('number', 'p200');			//5
	this.g_var.dataTable.addColumn('number', 'st');
	this.g_var.dataTable.addColumn('number', 'mt');
	this.g_var.dataTable.addColumn('number', 'lt');
	this.g_var.dataTable.addColumn('string', 'clo');
	this.g_var.dataTable.addColumn('string', 'chg');			//10
	this.g_var.dataTable.addColumn('string', 'mi');			//11
	this.g_var.dataTable.addColumn('string', 'ma');
	this.g_var.dataTable.addColumn('number', 'rgr');
	this.g_var.dataTable.addColumn('number', 'ngr');
	this.g_var.dataTable.addColumn('number', 'fgr');
	this.g_var.dataTable.addColumn('number', 'dr');				//16
	this.g_var.dataTable.addColumn('number', 'fa');
	this.g_var.dataTable.addColumn('number', 'rtar');
	this.g_var.dataTable.addColumn('number', 'roe');
	this.g_var.dataTable.addColumn('number', 'atr');
	this.g_var.dataTable.addColumn('number', 'itr');			//21
	this.g_var.dataTable.addColumn('number', 'gptr');
	this.g_var.dataTable.addColumn('number', 'per');
	this.g_var.dataTable.addColumn('number', 'psr');			//24
	this.g_var.dataTable.addColumn('string', 'prs');
	this.g_var.dataTable.addColumn('string', 'sup');
	this.passCnt = 0;
}

ObjStkDbTwse.prototype.Create = function(market) {
	var url_;
	alert(market);
	if (market == 1) {
		url_ = this.g_var.url_twse;
	} else if (market == 2) {
		url_ = this.g_var.url_otc;
	} else return;
	
	_IG_FetchContent(url_, _IG_Callback(Create_, this, market),  { refreshInterval: 0});
		//function(response) {	content = response; });
	
	function Create_(content, stkdb, market) {
		var stkNum = 0;
		var id = "", name = "";
		alert(content);
		//alert(stkdb.max);
		
		if (market == 1) {
			parag_txt1 = "<table width=590 border=0 align=center cellpadding=0 cellspacing=0>";
			parag_txt2 = "</table>";
			id_txt1 = "<td height=20 class=basic2 align=center>";
			id_txt2 = "</td>";
			name_txt1 = "<td height=20 class=basic2 align=center>";
			name_txt2 = "</td>";
		} else if (market == 2) {
			parag_txt1 = "<table id=\'contentTable\' cellpadding=\'0\' cellspacing=\'1\' border=\'0\' style=\'width:100%;\'>";
			parag_txt2 = "</table>";
			id_txt1 = "<TD nowrap class=\'table-body-left\'>";
			id_txt2 = "</td>";
			name_txt1 = "<td nowrap class=\'table-body-left\'>";
			name_txt2 = "</td>";
		}
		
		var enuSrh = new ObjEnumSearch(content);
		subcontent = enuSrh.substring_(parag_txt1, 0, parag_txt2, 0);
		var strTbl = new ObjEnumSearch(subcontent);
		while (1) {
			id = strTbl.substring_(id_txt1, 0, id_txt2, 0);
			//alert(id);
			if (id == "") break;
			name = strTbl.substring_(name_txt1, 0, name_txt2, 0);
			//alert(name);
			if (name == "") break;
			stkdb.objStk[stkNum] = new ObjStk(stkdb, stkdb.g_var);
			stkdb.objStk[stkNum].Init(id, name);
			
			//stkdb.objStk[stkNum].GetAnalysis();
			if (stkNum >= stkdb.max) break;
			++stkNum;
		}		
		//alert("ObjStkDb.Create() stk#: " + stkdb.objStk.length);
		stkdb.EnumFiltering();
	}	
}
ObjStkDbTwse.prototype.EnumFiltering = function() {
	if (this.filterIdx < this.objStk.length) {
		dbgMsg("["+this.filterIdx+"/"+this.objStk.length+"]"+" id:"+this.objStk[this.filterIdx].id, 1);
		this.objStk[this.filterIdx].GetAnalysis();
	} else {
		stkMsg("completed");
	}
}
/*
ObjStkDbTwse.prototype.GetAnalysis = function() {
	stkdbMsg("ObjStkDb.GetAnalysis() stk#: " + this.objStk.length);
	//alert("ObjStkDb.GetAnalysis");
	for (i=0; i < this.objStk.length; i++) {
		this.objStk[i].GetAnalysis();
	}
}
ObjStkDbTwse.prototype.Filter = function() {
	dbgMsg("ObjStkDb.Filter() stk#: " + this.objStk.length);
	var pass = 0;
	for (i=0; i < this.objStk.length; i++) {
	  if (this.objStk[i].analysis.shortStar >= 2 && this.objStk[i].analysis.medStar >= 2 && this.objStk[i].analysis.longStar >= 2
				//&& this.objStk[i].analysis.shortTrend > 0 && this.objStk[i].analysis.medTrend > 0
				//&& this.objStk[i].analysis.opt > 0
				//&& this.objStk[i].analysis.per200 > 50.0
				) {

			str1 = this.objStk[i].id + this.objStk[i].name;
			//dbgMsg(str1);
			str2 = this.objStk[i].analysis.shortStar + "/" +
						 this.objStk[i].analysis.medStar + "/" +
						 this.objStk[i].analysis.longStar;
			str3 = this.objStk[i].analysis.per200 + "%";
			str4 = "opt: " + this.objStk[i].analysis.opt;
			str5 = "trend: " + this.objStk[i].analysis.shortTrend + "/" + this.objStk[i].analysis.medTrend;
			str10 = "close: " + this.objStk[i].tradeInfo.close + ", " + this.objStk[i].tradeInfo.change;
			str11 = " (" + this.objStk[i].tradeInfo.maxOfYear + "-" + this.objStk[i].tradeInfo.minOfYear + ")";
			dbgMsg("["+i+"]"+str1 + "->" + str2 + "," + str3 + "," + str4 + "," + str5 + "," + str10 + str11); 
			pass++;
		}
	}
	dbgMsg("pass count: " + pass);
	_IG_AdjustIFrameHeight();
}
*/
//====================================================
function ObjTradeInfo() {
	this.close = 0;
	this.change = 0;
}
ObjTradeInfo.prototype.parse = function(content) {
	var enuSrh = new ObjEnumSearch(content);
	
	//alert(content.length);
	enuSrh.substring_("<td class=\"", 0, "\"", 0);
	this.close = enuSrh.substring_(">", 0, "</td>", 0);
	//alert(this.close);
	enuSrh.substring_("<td class=\"", 0, "\"", 0);
	this.change = enuSrh.substring_(">", 0, "</td>", 0);
	enuSrh.substring_("<td class=", 0, "</td>", 0);
	enuSrh.substring_("<td class=\"", 0, "\"", 0);
	this.maxOfYear = enuSrh.substring_(">", 0, "</td>", 0);
	enuSrh.substring_("<td class=", 0, "</td>", 0);
	enuSrh.substring_("<td class=\"", 0, "\"", 0);
	this.minOfYear = enuSrh.substring_(">", 0, "</td>", 0);
}
//====================================================
function ObjAnalysis() {
	this.ready = false;
	this.shortTrend = 0;
	this.shortStar = 0;
	this.medTrend = 0;
	this.medStar = 0;
	this.longTrend = 0;
	this.longStar = 0;
	this.light = 0;
	this.opt = 0;
	this.perPos = 0;
	this.perNeg = 0;
	this.per200 = 0;
}
ObjAnalysis.prototype.parse = function(content) {
	var enuSrh = new ObjEnumSearch(content);	

	var sign = 0;
	var st1 = "";

	//alert("parse()");
	enuSrh.substring_("winnerTable", 0, "</tr>", 0);
	// 短線偏?
	st1 = enuSrh.substring_("<td class=\"", 0, "\"", 0);
	if (st1 == "r")	sign = 1;
	else if (st1 == "g") sign = -1; 
	else if (st1 == "b") sign = 0;
	else return;
	this.shortTrend = sign;
	//alert("short: "+st1);
	
	// 短線(star)
	st1 = enuSrh.substring_("<td class=\"bg ", 0, "\"", 0);
	if (st1 == "r")	sign = 1;
	else sign = -1;
	st1 = enuSrh.substring_(">", 0, "</td>", 0);
	this.shortStar = sign * st1.length;
	
	// 這次做多(performance)
	st1 = enuSrh.substring_("<td class=\"bg ", 0, "\"", 0);
	st1 = enuSrh.substring_(">", 0, "</td>", 0);
	this.perPos = eval(st1.substr(0, st1.length-1));	
	
	//中線偏?
	st1 = enuSrh.substring_("<td class=\"", 0, "\"", 0);
	if (st1 == "r")	sign = 1;
	else if (st1 == "g") sign = -1; 
	else sign = 0;
	this.medTrend = sign;
	//alert("med: "+st1);
	
	// 中線(star)
	st1 = enuSrh.substring_("<td class=\"bg ", 0, "\"", 0);
	if (st1 == "r")	sign = 1;
	else sign = -1;
	st1 = enuSrh.substring_(">", 0, "</td>", 0);
	this.medStar = sign * st1.length;
	//alert("med: "+sign+", star: "+st1.length);
	
	// 進出策略
	st1 = enuSrh.substring_("<td class=\"", 0, " ", 0);
	if (st1 == "r")	sign = 1;
	else if (st1 == "g") sign = -1; 
	else sign = 0;
	this.opt = sign;
	
	// 這次做空(performance)
	st1 = enuSrh.substring_("<td class=\"bg ", 0, "\"", 0);
	st1 = enuSrh.substring_(">", 0, "</td>", 0);
	this.perNeg = eval(st1.substr(0, st1.length-1));
	//alert(this.perNeg);
	
	// 長線偏?
	st1 = enuSrh.substring_("<td class=\"", 0, "\"", 0);
	if (st1 == "r")	sign = 1;
	else if (st1 == "g") sign = -1; 
	else sign = 0;
	this.longTrend = sign;
	
	// 長線(star)
	st1 = enuSrh.substring_("<td class=\"bg ", 0, "\"", 0);
	if (st1 == "r")	sign = 1;
	else sign = -1;
	st1 = enuSrh.substring_(">", 0, "</td>", 0);
	this.longStar = sign * st1.length;
	
	// 200天績效
	st1 = enuSrh.substring_("<td class=\"bg ", 0, "\"", 0);
	st1 = enuSrh.substring_(">", 0, "</td>", 0);
	this.per200 = eval(st1.substr(0, st1.length-1));
	//alert(this.per200); 
	this.ready = true;
}